﻿#if DIRECTSHOW
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DirectShowLib;
using DirectShowLib.Utils;
using System.Diagnostics;
using System.Threading;

namespace WhatsPlayingClient.Audio
{
	class ConverterGraph
	{
		static readonly Guid guidFfdshowAudioProcessor = new Guid("B86F6BEE-E7C0-4D03-8D52-5B4430CF6C88");
		static readonly string monikerFfdshowAudioProcessor = "@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{0F40E1E5-4F79-4988-B1A9-CC98794E6B55}";
		static readonly Guid guidFileSource = new Guid("E436EBB5-524F-11CE-9F53-0020AF0BA770");
		static readonly Guid guidDefaultDirectSoundDevice = new Guid("79376820-07D0-11CF-A24D-0020AFD79767");

		IGraphBuilder graphBuilder;


		public ConverterGraph(string fileName)
		{
			graphBuilder = new FilterGraph() as IGraphBuilder;


			//graphBuilder.LoadGraphFile(@"D:\temp\Untitled.GRF");
			//(graphBuilder as IMediaControl).Run();
			//Thread.Sleep(100000);

			int res = graphBuilder.RenderFile(fileName, null);

			var source = graphBuilder.FindFilterByClsid(guidFileSource);

			var renderer = graphBuilder.FindFilterByClsid(guidDefaultDirectSoundDevice);
			var rendererInputPin = DsFindPin.ByConnectionStatus(renderer, PinConnectedStatus.Connected, 0);
			IPin pinToRender;
			Trace.WriteLine(string.Format("rendererInputPin: {0}", rendererInputPin.Stringify()));
			res = rendererInputPin.ConnectedTo(out pinToRender);
			Trace.WriteLine(string.Format("pinToRender: {0}", pinToRender.Stringify()));

			//res = rendererInputPin.Disconnect();
			//res = pinToRender.Disconnect();
			graphBuilder.Disconnect(pinToRender);
			graphBuilder.Disconnect(rendererInputPin);

			Trace.WriteLine(string.Format("rendererInputPin: {0}", rendererInputPin.Stringify()));
			Trace.WriteLine(string.Format("pinToRender: {0}", pinToRender.Stringify()));

			graphBuilder.RemoveFilter(renderer);

			//			graphBuilder.ConnectFilters(pinToRender, rendererInputPin, false);

			//if(!FilterGraphTools.IsThisComObjectInstalled(guidFfdshowAudioProcessor))
			//    throw new NotSupportedException("ffdshow Audio Processor not found");
			//var processor = graphBuilder.AddFilterByDevicePath(monikerFfdshowAudioProcessor, "Audio Processor");

			//var processorInputPin = DsFindPin.ByDirection(processor, PinDirection.Input, 0);
			//var processorOutputPin = DsFindPin.ByDirection(processor, PinDirection.Output, 0);

			//Trace.WriteLine(string.Format("processorOutputPin: {0}", processorOutputPin.Stringify()));

			//graphBuilder.ConnectFilters(processorOutputPin, rendererInputPin, false);

			//Trace.WriteLine(string.Format("processorInputPin: {0}", processorInputPin.Stringify()));

			//graphBuilder.ConnectFilters(pinToRender, processorInputPin, false);

			//res = graphBuilder.Render(processorOutputPin);

			var grabberCB = new SampleGrabber();
			var nullRenderer = (IBaseFilter)new NullRenderer();
			var grabber = (IBaseFilter)grabberCB.Filter;

			graphBuilder.AddFilter(grabber, "Grabber");
			graphBuilder.AddFilter(nullRenderer, "Null Renderer");
						
			var grabberInPin = DsFindPin.ByDirection(grabber, PinDirection.Input, 0);
			var grabberOutPin = DsFindPin.ByDirection(grabber, PinDirection.Output, 0);
			var nullRendererInPin = DsFindPin.ByDirection(nullRenderer, PinDirection.Input, 0);

			graphBuilder.ConnectFilters(pinToRender, grabberInPin, false);
			graphBuilder.ConnectFilters(grabberOutPin, nullRendererInPin, false);

			var mediaControl = graphBuilder as IMediaControl;
			res = mediaControl.Run();
			
			System.Threading.Thread.Sleep(100000);
		}
	}
}
#endif
